float4x4 WorldViewProj : WorldViewProjection;

float BlurWidth: FloatParameter0 = 0.01f;
float Desaturate: FloatParameter1 = 25;
float MinSaturation: FloatParameter2 = 0.25f;
float OriginalPixelWeight: FloatParameter3 = 24;
float2 QuadScreenSize = {1024,768};

float2 Center: Float4Parameter0 = { 0.5f, 0.5f };

static const float3 LUMINANCE_VECTOR  = float3(0.2125f, 0.7154f, 0.0721f);

struct VS_OUTPUT_FAST
{
   	float4 Position    : POSITION;
    float2 TexCoord[8] : TEXCOORD0;
};

struct VS_OUTPUT
{
   	float4 Position    : POSITION;
    float2 UV : TEXCOORD0;
};

texture Tex0 : Texture0;
sampler2D SamplerTex0 = sampler_state{  Texture = (Tex0);    MIPFILTER = NONE;    MAGFILTER = POINT;    MINFILTER = POINT;  ADDRESSU = CLAMP; ADDRESSV = CLAMP;};


////////////////////////////////// vertex shaders //////////

VS_OUTPUT VS_RadialBlur(float4 Position : POSITION, 
				  		float2 TexCoord : TEXCOORD0)
{
    VS_OUTPUT OUT;
    OUT.Position=mul(Position, WorldViewProj);
	//OUT.Position = Position;
 	float2 texelSize = 1.0 / QuadScreenSize;
    OUT.UV = TexCoord + texelSize*0.5f - Center;
    return OUT;
}

//////////////////////////////////////////////////////
////////////////////////////////// pixel shaders /////
//////////////////////////////////////////////////////

half4 PS_RadialBlur(VS_OUTPUT IN,
			   		uniform int nsamples
			   		) : COLOR
{
	float4 originalPixel = tex2D(SamplerTex0, IN.UV.xy + Center );
	
	half4 c = 0;
    // this loop will be unrolled by compiler and the constants precalculated:
	c+=originalPixel*OriginalPixelWeight;
	for(int i=1; i<(nsamples-1); i++) {
    	//float scale = 1 + BlurWidth*(i/(float) (nsamples-1))*length(IN.UV);
		float scale = 1 - BlurWidth*(pow(i,1.2f)/(float) (nsamples-1));
    	c += tex2D(SamplerTex0, IN.UV.xy*scale + Center );
   	}
   	c /= nsamples-1+OriginalPixelWeight;
    float luma = dot(c.xyz,LUMINANCE_VECTOR);
	float saturation  = lerp(1,MinSaturation,saturate(length(IN.UV)*BlurWidth*Desaturate));
	return float4(lerp(luma.xxx,c.xyz,saturation),1);
    return float4(c.xyz,1);
} 

/////////////////////////////////////// techniques /////////

technique RadialBlur  {
    pass p0  {
		cullmode = none;
		ZEnable = false;
		AlphaBlendEnable = false;
		
		VertexShader = compile vs_3_0 VS_RadialBlur();
		PixelShader  = compile ps_3_0 PS_RadialBlur(32);
    }
}
